package Chapter2.Comparing.c9;

public class SellTickets {
    public static void main(String[] args) {
        Sell sell = new Sell();
        Thread th1 = new Thread(sell,"No.1 window");
        Thread th2 = new Thread(sell,"No.2 window");
        Thread th3 = new Thread(sell,"No.3 window");
        th1.start();th2.start();th3.start();

    }
}

class Sell implements Runnable{
    private int tickets = 20;


    @Override
    public void run() {
        int c = 0;
        /*while (tickets>0) {
            c++;
            System.out.println(Thread.currentThread().getName()+"卖第"+c);
            tickets--;
        }*/
        while (true) {
            synchronized (this) {
                if(tickets<=0){
                    break;
                }
                tickets--;

            }
            c++;
            System.out.println(Thread.currentThread().getName()+"还剩"+tickets);
        }
    }
}
